package com.yanqu.road.server.redis;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.logic.redis.RedisKeyDefine;
import com.yanqu.road.reids.subpub.ReliableMessageSubPub;
import com.yanqu.road.reids.subpub.eRedisChannelType;
import com.yanqu.road.server.manger.ChannelSystemConfigMgr;
import com.yanqu.road.server.manger.ForbiddenWordOverSeasMgr;
import com.yanqu.road.server.manger.SystemLoopMgr;

public class SystemConfigRedisSubPub implements ReliableMessageSubPub {
    String channel = eRedisChannelType.SystemConfig.getChannel();

    @Override
    public void onMessage(String message) {
        JSONObject jsonObject;
        try{//
            jsonObject = JSONObject.parseObject(message);
        }catch (Exception e){
            return;
        }
        String publishKey = jsonObject.getString("type");
        switch (publishKey) {
            case RedisKeyDefine.CHANNEL_SYSTEM_CONFIG_PUBLISH: {
                int channelId = jsonObject.getIntValue("channelId");
                ChannelSystemConfigMgr.reloadDataFromRedis(channelId);
            }
            break;
            case RedisKeyDefine.CHANNEL_SYSTEM_LOOP_PUBLISH: {
                int channelId = jsonObject.getIntValue("channelId");
                SystemLoopMgr.reloadDataFromRedis(channelId);
            }
            break;
            case RedisKeyDefine.FORBIDDEN_WORD_PUBLISH: {
                ForbiddenWordOverSeasMgr.loadDb();
            }
            break;
        }
    }

    @Override
    public boolean isSupport(String channel) {
        return this.channel.equals(channel);
    }

    @Override
    public String getChannel() {
        return this.channel;
    }

    @Override
    public String getKey(String message) {
        return eRedisChannelType.SystemConfig.getMessageKey(message);
    }
}
